package com.chen.webservice.Utils;

import com.chen.webservice.service.ArrangeService;
import com.chen.webservice.model.Order;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description: 规则4 包屏类节目指定时间播放
 * @params:
 * @return:
 * @author: chenzhiwen
 * @dateTime: 2021/11/8 下午7:16
 */
@Service
public class CoverHandler implements RuleHandler {
    private static final int factor = 10;//系数

    private static final int ruleNum = 4;

    @Override
    public int ruleNum() {
        return this.ruleNum;
    }

    @Override
    public boolean isMatch(List<Integer> rules) {
        return true;//强校验
    }

    @Override
    public int checkResult(List<Order> orders, int rate) {
        rate = 0;
        int len = orders.size();
        if (len <= 1) {
            return rate;
        }
        for (int i = 0; i < len - 1; i++) {
            Order o1 = orders.get(i);
            Order o2 = orders.get(i + 1);
            if (o1.getCategorySub() == RuleConstants.CateType.CAR.getValue()
                    && o2.getCategorySub() == RuleConstants.CateType.WINE.getValue()
                    || o2.getCategorySub() == RuleConstants.CateType.CAR.getValue()
                    && o1.getCategorySub() == RuleConstants.CateType.WINE.getValue()) {
                rate += 1;
            }
        }
        return rate * factor;
    }

    public int checkResultWithCover(List<Order> orders, List<Order> covOrders, List<Order> oriOrders, int startTime) {
        int rate = 0;
        int len = orders.size();
        if (len <= 1) {
            return rate;
        }
        for (int i = 0; i < len ; i++) {
            Order o1 = orders.get(i);
            if (o1.getPlayType() == RuleConstants.PlayType.COVER.getValue()) {
                int coverTime = o1.getStartTime();
                int played = ArrangeService.getPlayedTime(orders.subList(0, i)) + startTime;
                rate += Math.abs(factor * (coverTime - played));
            }
        }
        return rate;
    }
}
